package com.wang.springbootinit.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.springbootinit.constant.CommonConstant;
import com.wang.springbootinit.constant.UserConstant;
import com.wang.springbootinit.model.dto.chart.ChartQueryRequest;
import com.wang.springbootinit.model.entity.Chart;
import com.wang.springbootinit.model.entity.User;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class ChartQueryWrapperUtils {

    /**
     * 获取查询包装类
     *
     * @param chartQueryRequest
     * @return
     */
    public static QueryWrapper<Chart> getQueryWrapper(ChartQueryRequest chartQueryRequest, User user) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        if (chartQueryRequest == null) {
            return queryWrapper;
        }
        Long id = chartQueryRequest.getId();
        String name = chartQueryRequest.getName();
        String goal = chartQueryRequest.getGoal();
        String chartType = chartQueryRequest.getChartType();
        Long userId = chartQueryRequest.getUserId();
        String sortField = chartQueryRequest.getSortField();
        String sortOrder = chartQueryRequest.getSortOrder();

        queryWrapper.eq(id != null && id > 0, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(StringUtils.isNotBlank(goal), "goal", goal);
        queryWrapper.eq(StringUtils.isNotBlank(chartType), "chartType", chartType);
        if(!user.getUserRole().equals(UserConstant.ADMIN_ROLE)){
            //普通用户
            queryWrapper.eq(ObjectUtils.isNotEmpty(user.getId()), "userId", user.getId());
        }

        queryWrapper.eq("isDelete", false);
//        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        queryWrapper.orderByDesc("createTime");
        return queryWrapper;
    }

}
